﻿using SearchStarPlus.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace SearchStarPlus.Controllers
{
    public class HomeController : Controller
    {
        ArticleUIController artCon = new ArticleUIController("http://localhost:3178/", "api/article/");
        KeywordExclusionController keyCon = new KeywordExclusionController("http://localhost:3178/", "api/keywordexclusion/");
        List<Article> articles = new List<Article>();
        List<KeywordExclusion> words = new List<KeywordExclusion>();
        int arrayindex;
       
        public ActionResult Index()
        {      
            return View();
        }

        public ActionResult About()
        {
            words = keyCon.Get();
            return View(words);
        }

        public ActionResult Search(string q)
        {   
            List<String> searchList = new List<String>();

            if (!String.IsNullOrWhiteSpace(q))
            {
                // Break up user entry into tokens based on white space.
                searchList = (Regex.Split(q.Trim(), @"\s+")).ToList<String>();

                // Remove duplicates from list.
                searchList = searchList.Distinct().ToList();
            }

            if (q.Length == 0)
            {
                Session["searchWord"] = q;
                articles = artCon.Get();
                Session["Results"] = articles;
            }
            else
            {
                
                IEnumerable<KeywordExclusion> exclusions = keyCon.Get(); // get exclusion list
                List<String> exclusionWords = new List<string>();
                foreach (KeywordExclusion item in exclusions)
                {
                    exclusionWords.Add(item.Word);
                }
                searchList = searchList.Except(exclusionWords, StringComparer.OrdinalIgnoreCase).ToList();

                Session["searchWord"] = searchList;
                articles = artCon.SearchArticles(searchList);
                Session["Results"] = articles;
            }
            arrayindex = 0;  // resets it to 0
            ViewBag.Size = articles.Count();
            Session["ArrayIndex"] = arrayindex;
            ViewBag.Index = arrayindex + 1;
            ViewBag.searchWord = Session["searchWord"];
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult First()
        {
            List<Article> articles = (List<Article>)Session["Results"];
            int arrayindex = 0;
            Session["ArrayIndex"] = 0;
            ViewBag.Size = articles.Count();
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult Previous()
        {
            List<Article> articles = (List<Article>)Session["Results"];
            int arrayindex = (int)Session["ArrayIndex"];
            if (arrayindex == 0)
            {
                ViewBag.Size = articles.Count();
                ViewBag.Index = arrayindex + 1;
                ViewBag.Description = articles[arrayindex].Description;
                ViewBag.SearchResults = articles[arrayindex].Text;
                return PartialView("_SearchArticlePartial", articles);
            }
            
            arrayindex--;
            Session["ArrayIndex"] = arrayindex;
            ViewBag.Size = articles.Count();
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult Next()
        {
            List<Article> articles = (List<Article>)Session["Results"];
            int arrayindex = (int)Session["ArrayIndex"];

            if ((arrayindex+1) == articles.Count())
            {
                ViewBag.Size = articles.Count();
                ViewBag.Index = arrayindex + 1;
                ViewBag.Description = articles[arrayindex].Description;
                ViewBag.SearchResults = articles[arrayindex].Text;
                return PartialView("_SearchArticlePartial", articles);
            }

            arrayindex++;
            Session["ArrayIndex"] = arrayindex;
            ViewBag.Size = articles.Count();
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult Last()
        {
            List<Article> articles = (List<Article>)Session["Results"];
            Session["ArrayIndex"] = articles.Count() - 1;
            int arrayindex = articles.Count() - 1;
            ViewBag.Size = articles.Count();
            ViewBag.Index = arrayindex + 1;
            ViewBag.Description = articles[arrayindex].Description;
            ViewBag.SearchResults = articles[arrayindex].Text;
            return PartialView("_SearchArticlePartial", articles);
        }

        public ActionResult GetHighlight()
        {
            //List<Article> articles = (List<Article>)Session["Results"];
            var searchWord = (string) Session["searchWord"];
            return Content(searchWord, "text/plain");
            //return null;
        }

        public ActionResult AddWord(string e)
        {
            KeywordExclusion newWord = new KeywordExclusion();
            newWord.Word = e;

            ViewBag.ResultMsg = keyCon.PostInsert(newWord);

            words = keyCon.Get();
            return PartialView("_KeywordExclusionPartial", words);
        }

        public ActionResult UpdateWord(string id, string word)
        {
            KeywordExclusion update = new KeywordExclusion();
            update.ExclusionId = Convert.ToInt32(id);
            update.Word = word;

            ViewBag.ResultMsg = keyCon.PutUpdate(update);

            words = keyCon.Get();
            return PartialView("_KeywordExclusionPartial", words);
        }

        public ActionResult DeleteWord(string g)
        {
            ViewBag.ResultMsg = keyCon.Delete(Convert.ToInt32(g));

            words = keyCon.Get();
            return PartialView("_KeywordExclusionPartial", words);
        }
    }
}
